package numberslider;

import dbutils.DbUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Dec 23, 2009
 * Time: 12:37:59 AM
 * To change this template use File | Settings | File Templates.
 */
public class ResultSetFrame extends JFrame implements ActionListener {
    public ResultSetFrame() {
        setTitle("ResultSet");
        setSize(300, 200);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        /* find all tables in the database and add them to
           a combo box
        */

        Container contentPane = getContentPane();
        tableNames = new JComboBox();
        tableNames.addActionListener(this);
        JPanel p = new JPanel();
        p.add(tableNames);
        contentPane.add(p, "North");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            // force loading of driver
            String url = "jdbc:mysql://localhost/"+ DbUtils.GENERAL_DATABASE_NAME;
            String user = "root";
            String password = "root";
            con = DriverManager.getConnection(url, user,
                    password);
            if (SCROLLABLE)
                stmt = con.createStatement(
                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY);
            else
                stmt = con.createStatement();
            DatabaseMetaData md = con.getMetaData();
            ResultSet mrs = md.getTables(null, null, null, new String[]{"TABLE"});
            while (mrs.next())
                tableNames.addItem(mrs.getString(3));
            mrs.close();
        }
        catch (ClassNotFoundException e) {
            System.out.println("Error " + e);
        }
        catch (SQLException e) {
            System.out.println("Error " + e);
        }
    }

    public void actionPerformed(ActionEvent evt) {
        if (evt.getSource() == tableNames) {  // show the selected table from the combo box

            if (scrollPane != null)
                getContentPane().remove(scrollPane);
            try {
                String tableName
                        = (String) tableNames.getSelectedItem();
                if (rs != null) rs.close();
                String query = "SELECT * FROM " + tableName;
                rs = stmt.executeQuery(query);

                if (SCROLLABLE) {
                    model = new ScrollingResultSetTableModel(rs);
                } else {
                    model = new CachingResultSetTableModel(rs);
                }

                JTable table = new JTable(model);
                scrollPane = new JScrollPane(table);
                getContentPane().add(scrollPane, "Center");
                pack();
                doLayout();
                validate();
            }
            catch (SQLException e) {
                System.out.println("Error " + e);
            }
        }
    }

    private JScrollPane scrollPane;
    private ResultSetTableModel model;
    private JComboBox tableNames;
    private JButton nextButton;
    private JButton previousButton;
    private ResultSet rs;
    private Connection con;
    private Statement stmt;

    private static boolean SCROLLABLE = false;
    // set to true if your database supports scrolling cursors
}

